package game.module.itemnew.dao;

import game.common.BeanTool;
import game.constants.GameConstant;
import game.module.itemnew.beans.ItemTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

@Repository
@Lazy(value = false)
public class ItemTemplateCache {

	public static final Logger logger = LoggerFactory.getLogger(ItemTemplateCache.class);

	@Autowired
	private ItemTemplateDao itemTemplateDao;

	@Autowired
	private CacheManager cacheManager;

	@PostConstruct
	public void reload() {
		try {
			initItemTemplate();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	@SuppressWarnings({ "unchecked" })
	private void initItemTemplate() {
		try {
			List<ItemTemplate> itemTpllist = itemTemplateDao.getAllTemplate();

			Cache cache = cacheManager.getCache(GameConstant.CACHE_ITEM_TEMPLATE);
			ValueWrapper idKeyGoods = cache.get(GameConstant.CACHE_ITEM_TEMPLATE_ID_KEY_GOODS);
			Map<Integer, ItemTemplate> idKeyGoodsMap = null;
			if (idKeyGoods != null) {
				idKeyGoodsMap = (Map<Integer, ItemTemplate>) idKeyGoods.get();
			} else {
				idKeyGoodsMap = new HashMap<Integer, ItemTemplate>();
				cache.put(GameConstant.CACHE_ITEM_TEMPLATE_ID_KEY_GOODS, idKeyGoodsMap);
			}
			BeanTool.addOrUpdate(idKeyGoodsMap, itemTpllist, "id");
			ValueWrapper nameKeyGoods = cache.get(GameConstant.CACHE_ITEM_TEMPLATE_NAME_KEY_GOODS);
			Map<String, ItemTemplate> nameKeyGoodsMap = null;
			if (idKeyGoods != null) {
				nameKeyGoodsMap = (Map<String, ItemTemplate>) nameKeyGoods.get();
			} else {
				nameKeyGoodsMap = new HashMap<String, ItemTemplate>();
				cache.put(GameConstant.CACHE_ITEM_TEMPLATE_NAME_KEY_GOODS, idKeyGoodsMap);
			}
			BeanTool.addOrUpdate(nameKeyGoodsMap, itemTpllist, "name");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	@SuppressWarnings("unchecked")
	public ItemTemplate getItemTplById(int id) {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_ITEM_TEMPLATE);
		ValueWrapper idKeyGoods = cache.get(GameConstant.CACHE_ITEM_TEMPLATE_ID_KEY_GOODS);
		if (idKeyGoods != null) {
			Map<Integer, ItemTemplate> idKeyMap = (Map<Integer, ItemTemplate>) idKeyGoods.get();
			return idKeyMap.get(id);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public ItemTemplate getItemTplByName(String name) {
		Cache cache = cacheManager.getCache(GameConstant.CACHE_ITEM_TEMPLATE);
		ValueWrapper idNameGoods = cache.get(GameConstant.CACHE_ITEM_TEMPLATE_NAME_KEY_GOODS);
		if (idNameGoods != null) {
			Map<String, ItemTemplate> idNameMap = (Map<String, ItemTemplate>) idNameGoods.get();
			return idNameMap.get(name);
		}
		return null;
	}

}
